#include "frmlogo.h"
#include "ui_frmlogo.h"

#include <QMessageBox>


FrmLogo::FrmLogo(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FrmLogo)
{
    ui->setupUi(this);

    connect(&_proc, SIGNAL(paraFrente(int)), ui->canvas, SLOT(paraFrente(int)));
    connect(&_proc, SIGNAL(paraTras(int)), ui->canvas, SLOT(paraTras(int)));
    connect(&_proc, SIGNAL(girarDireita(int)), ui->canvas, SLOT(girarDireita(int)));
    connect(&_proc, SIGNAL(girarEsquerda(int)), ui->canvas, SLOT(girarEsquerda(int)));
    connect(&_proc, SIGNAL(pular(int)), ui->canvas, SLOT(pular(int)));
    connect(&_proc, SIGNAL(forca(int)), ui->canvas, SLOT(forca(int)));
    connect(&_proc, SIGNAL(limpar()), ui->canvas, SLOT(limpar()));
    connect(&_proc, SIGNAL(mudarCor(QColor)), ui->canvas, SLOT(mudarCor(QColor)));
    connect(&_proc, SIGNAL(erro(QString)), this, SLOT(_erroProc(QString)));

    connect(ui->btnRun, SIGNAL(clicked()), this, SLOT(_btnExecutarClicked()));

    connect(ui->lneCommand, SIGNAL(returnPressed()), this, SLOT(_runSingleCommand()));

    ui->lneCommand->setVisible(false);

    /*
    QString s;
    s.append("PF 50\n");
    s.append("PL 10\n");
    s.append("GD 90\n");
    s.append("PF 50\n");
    s.append("PL 10\n");
    s.append("GD 90\n");
    s.append("PF 50\n");
    s.append("PL 10\n");
    s.append("GD 90\n");
    s.append("PF 50\n");
    */

    //ui->txtProgram->appendPlainText(s);

    //_proc.processLine("PF 50");


}

FrmLogo::~FrmLogo()
{
    delete ui;
}

void FrmLogo::_btnExecutarClicked()
{


    bool checked = ui->chkStepByStep->isChecked();

    _proc.processText( ui->txtProgram->toPlainText() );

    ui->canvas->desenhaTudo(checked);

}

void FrmLogo::_erroProc(QString msg)
{
    QMessageBox::critical(this, "Erro lendo comandos", msg);
}

void FrmLogo::_runSingleCommand()
{
    QString cmd = ui->lneCommand->text().trimmed();
    ui->lneCommand->selectAll();

    if( !cmd.isEmpty() )
        _proc.processText(cmd);

    ui->canvas->desenhaTudo(false);
}
